﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public partial class manage_manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string tmpdirectory = Server.MapPath(".") + "//userdata//";
        foreach (string sub in Directory.GetDirectories(tmpdirectory))
        {
            user tmpuser = new user(sub+"\\user.xml");
            DateTime tmpTime = Convert.ToDateTime(tmpuser.Crtm);
            if (DateTime.Now.CompareTo(tmpTime.AddDays(2))<0 )//如果是两天内新建的文件，则不删除文件夹.
                continue;

            if (sub.IndexOf("originalfirst") > 0 || sub.IndexOf("originalsec") > 0)//如果文件夹是"originalfirst" 或"originalsec"则不删
                continue;

            if (tmpuser.Ps != "49!3aU#c")//如果密码修改了，则不删.
                continue;

            Directory.Delete(sub,true);
           
        }
    }
    protected void importXMLToSQL_Click(object sender, EventArgs e)
    {
        SqlConnection conn=new SqlConnection("Data Source=HP-CQ57\\SQLEXPRESS;Initial Catalog=webCelloction;Integrated Security=True;Pooling=False");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        conn.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "webRank_Add_produce";
        SqlParameter pWebName = new SqlParameter();
        pWebName.ParameterName = "@name";
        pWebName.SqlDbType = SqlDbType.NChar;

        SqlParameter pWebLink = new SqlParameter();
        pWebLink.ParameterName = "@link";
        pWebLink.SqlDbType = SqlDbType.NVarChar;

        SqlParameter pWebRank = new SqlParameter();
        pWebRank.ParameterName = "@rank";
        pWebRank.SqlDbType = SqlDbType.SmallInt;

        SqlParameter pWebNote = new SqlParameter();
        pWebNote.ParameterName = "@note";
        pWebNote.SqlDbType = SqlDbType.NVarChar;
        
        string tmpdirectory = Server.MapPath(".") + "//userdata//";
        foreach (string sub in Directory.GetDirectories(tmpdirectory))
        {
            FileInfo fi = new FileInfo(sub + "//pageSerialized.xml");
            DateTime dt = fi.LastWriteTime;
            if (DateTime.Now.CompareTo(dt.AddDays(20)) < 0)//如果文件在最近20天内被修改过.
            {
                Xmlserdeser xser = new Xmlserdeser(HttpContext.Current.Server.MapPath("") + "//userdata//", sub.Substring(sub.LastIndexOf("//")+1),"main");
                for(int k=0;k<xser.Mpage.Mtables.Count; k++)
                {
                    table tmpTable = xser.Mpage.Mtables[k];
                    for (int i = 0; i < xser.Mpage.Mtables.Count; i++)
                    {
                        row tmpRow = tmpTable.Mrows[i];
                        if (tmpRow == null)
                        {
                            conn.Close();
                            return;
                        }
                        for (int j = 0; j < tmpRow.Mrowwebs.Count; j++)
                        {
                            web tmpWeb = tmpRow.Mrowwebs[j];
                            pWebName.Value = tmpWeb.Name;
                            pWebLink.Value = tmpWeb.Link;
                            pWebRank.Value = 1;
                            pWebNote.Value = tmpWeb.Note;
                            cmd.Parameters.Clear();
                            cmd.Parameters.Add(pWebName);
                            cmd.Parameters.Add(pWebLink);
                            cmd.Parameters.Add(pWebRank);
                            cmd.Parameters.Add(pWebNote);
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
        conn.Close();
    }
}